local assets = {
    Asset("ANIM", "anim/spear_obsidian.zip"),
    Asset("ANIM", "anim/swap_spear_obsidian.zip"),
}

SuicideCharacters = {}

local function OnEquip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_spear_obsidian", "swap_spear")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
end

local function OnUnequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    owner.AnimState:ClearOverrideSymbol("swap_object")
end

local function CalculateDamage(inst, attacker, target)
    if attacker:HasTag("witch") then
        return TUNING.ENERGY_DOMINATOR_WITCH_DAMAGE
    end

    return TUNING.ENERGY_DOMINATOR_DAMAGE
end

local function Suicide(inst, doer)
    if not doer:IsValid() then
        return
    end

    if not IsEntityDead(doer) then
        SuicideCharacters[doer.prefab] = true
        doer.components.health:DoDelta(-doer.components.health.maxhealth)
    end

    if not inst.suicide then
        inst.suicide = true
        inst.components.weapon:SetDamage(CalculateDamage)
    end
end

local function OnSave(inst, data)
    data.suicide = inst.suicide
end

local function OnLoad(inst, data)
    if not data then
        return
    end

    inst.suicide = data.suicide
    if inst.suicide then
        inst.components.weapon:SetDamage(CalculateDamage)
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    inst.AnimState:SetBuild("spear_obsidian")
    inst.AnimState:SetBank("spear_obsidian")
    inst.AnimState:PlayAnimation("idle")

    MakeInventoryPhysics(inst)

    inst:AddTag("sharp")
    inst:AddTag("pointy")
    -- weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")
    inst:AddTag("suicidal")

    MakeInventoryFloatable(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.suicide = false

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("weapon")

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)

    MakeHauntableLaunch(inst)

    inst.Suicide = Suicide

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    return inst
end

return Prefab("beautiful_dream", fn, assets)
